import { useRequest } from "ahooks";
import {Button, Form, Input, Spin, message} from "antd";
import { getQuery } from "../../../framework/common/utils";
import request from "../../../framework/common/utils/request";

function getSuperWorkspace(){
  return new Promise((resolve)=>{
      request({
          url: "/platform/setting/get-setting?token="+getQuery().token+"&type=superWorkspace",
          method: "post",
      }).then(
          function (data) {
              resolve(data.data)
          },
          function (error) {
              message.error({
                  content: "保存失败",
              });
          }
      );
  })

}

export default function () {
  const { data, error, loading } = useRequest(getSuperWorkspace, { defaultParams: [getQuery("string")] });
  if(loading){
      return <div className="flex items-center justify-center h-full"><Spin tip="加载中..."></Spin></div>
  }

    const onFinish = (values) => {
        request({
            url: "/platform/setting/save-setting?token="+getQuery().token+"&type=superWorkspace",
            method: "post",
            data:values
        }).then(
            function (data) {

                message.success("保存成功");
            },
            function (error) {
                message.error({
                    content: "保存失败",
                });
            }
        );
    };
    const onFinishFailed = (errorInfo) => {
        console.log("Failed:", errorInfo);
    };
  return (
    <Form
      className="flex flex-col justify-between h-full overflow-y-auto"
      name="basic"
      labelCol={{
        flex: "120px",
      }}
      initialValues={data}
      labelAlign="left"
      colon={false}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <div>
          <Form.Item label="应用ID" name="app_id" rules={[{ required: true, message: "请输入应用ID" }]}>
              <Input placeholder="请输入应用ID" />
          </Form.Item>
        <Form.Item label="JWT KEY" name="jwt_key" rules={[{ required: true, message: "请输入JWT KEY" }]}>
            <Input type="text" placeholder="请输入JWT KEY (默认alg=HS256) " />
        </Form.Item>
        <Form.Item label="第三方颁发Key" name="third_key" rules={[{ required: true, message: "请输入第三方颁发Key" }]}>
            <Input placeholder="请输入第三方颁发Key" />
        </Form.Item>
      </div>
       <div className="text-center">
        <Button type="primary" htmlType="submit">
          保存
        </Button>
      </div>
    </Form>
  );
}
